package cn.ydxiaoshuai.convert.controller;

import cn.ydxiaoshuai.convert.util.Base64Util;
import cn.ydxiaoshuai.convert.vo.BaseBean;
import cn.ydxiaoshuai.convert.vo.ImageBase64Bean;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;

/**
 * @author 小帅丶
 * @Description 图片转base64
 * @className ImageToBase64Controller
 * @Date 2019/7/18-14:35
 **/
@Controller
@RequestMapping(value = "/convert")
public class ImageToBase64Controller {

    private static final Logger log = LogManager.getLogger(ImageToBase64Controller.class);
    @Value(value = "${server.author}")
    private String author;

    /**
     * @param file    文件
     * @param request
     * @Description 上传的图片转base64
     * @Author 小帅丶
     * @Date 2019/7/18 15:08
     * @return org.springframework.http.ResponseEntity<java.lang.Object>
     **/
    @RequestMapping(value = "/base64/v1", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE}, method = {RequestMethod.POST})
    public ResponseEntity<Object> uploadDetectFace(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        ImageBase64Bean bean = new ImageBase64Bean();
        bean.setAuthor(author);
        ImageBase64Bean.Data data = new ImageBase64Bean.Data();
        String userAgent = request.getHeader("user-agent");
        log.info("请求的IP" + request.getRemoteAddr() + ",浏览器" + userAgent);
        try {
            if (!file.isEmpty()) {
                String fileNameOriginal = file.getOriginalFilename();
                String mimeType = request.getServletContext().getMimeType(fileNameOriginal);
                if (mimeType.startsWith("image/")) {
                    ByteArrayInputStream bais = new ByteArrayInputStream(file.getBytes());
                    BufferedImage image = ImageIO.read(bais);
                    if (null == image) {
                        bean.setCode(621);
                        bean.setMsg(" image format error");
                        bean.setMsg_zh("不支持svg格式");
                    } else {
                        String base64 = Base64Util.encode(file.getBytes());
                        data.setBase64(base64);
                        data.setHeight(image.getHeight());
                        data.setWidth(image.getWidth());
                        data.setSize(file.getSize());
                        data.setFilename(file.getOriginalFilename());
                        bean.setData(data);
                        bean.setCode(200);
                        bean.setMsg("convert successful");
                        bean.setMsg_zh("转换成功");
                    }
                } else {
                    bean.setCode(621);
                    bean.setMsg(" image format error");
                    bean.setMsg_zh("非图片格式");
                }
            } else {
                bean.setCode(601);
                bean.setMsg("empty image");
                bean.setMsg_zh("图片为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            bean.setCode(500);
            bean.setMsg("Internal Server Error");
            bean.setMsg_zh("系统错误，请稍后再试");
        }
        return new ResponseEntity<Object>(bean, httpHeaders, HttpStatus.OK);
    }
}
